<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>http-stream</title>
</head>

<body>
  <span>http stream</span>
  <button onclick="javascript:history.back()">back</button>

  <script type="text/javascript">
    function createStreamClient(url, progress, done) {
      //received为接收到数据的计数器
      var xhr = new XMLHttpRequest(),
        received = 0;
      xhr.open("get", url, true);
      xhr.onreadystatechange = function () {
        var result;
        if (xhr.readyState == 3) {
          //console.log(xhr.responseText);
          result = xhr.responseText.substring(received);
          received += result.length;
          progress(result);
        } else if (xhr.readyState == 4) {
          done(xhr.responseText);
        }
      };
      xhr.send(null);
      return xhr;
    }
    var client = createStreamClient("http://localhost:8088/stream", function (data) {
      console.log("Received:" + data);
    }, function (data) {
      console.log("Done,the last data is:" + data);
    })
  </script>
</body>

</html>
